package com.bootdo.contract.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.bootdo.contract.domain.UserMissionDO;
import com.bootdo.contract.service.UserMissionService;
import com.bootdo.contract.utils.ObjectUtils;
import com.bootdo.system.domain.RoleDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.contract.domain.MissionDO;
import com.bootdo.contract.service.MissionService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 任务表
 * 
 * @author yang
 * @email 2795210596@qq.com
 * @date 2019-04-26 12:35:53
 */
 
@Controller
@RequestMapping("/contract/mission")
public class MissionController {
	@Autowired
	private MissionService missionService;

	@Autowired
	UserService userService;
	@Autowired
	UserMissionService userMissionService;
	
	@GetMapping()
	@RequiresPermissions("contract:mission:mission")
	String Mission(){
	    return "contract/mission/mission";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("contract:mission:mission")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MissionDO> missionList = missionService.list(query);
		int total = missionService.count(query);
		PageUtils pageUtils = new PageUtils(missionList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("contract:mission:add")
	String add(Model model){
		List<UserDO> userList = userService.list();
		model.addAttribute("userList", userList);
	    return "contract/mission/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("contract:mission:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MissionDO mission = missionService.get(id);
		model.addAttribute("mission", mission);

		Map<String,Object>map = new HashMap<>();
		map.put("missionid",mission.getId());
		List<UserMissionDO> UserMissionDOList = userMissionService.list(map);
		List<UserDO> userList = userService.list();
		if (UserMissionDOList != null){
			for (UserDO userDO : userList) {
				userDO.setSign("false");
				for (UserMissionDO userMissionDO : UserMissionDOList) {
					if (Objects.equals(userDO.getUserId(), userMissionDO.getUserid())) {
						userDO.setSign("true");
						break;
					}
				}
			}
		}
		model.addAttribute("userList", userList);
	    return "contract/mission/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("contract:mission:add")
	public R save( MissionDO mission){
		mission.setStatus(0l);
		if(missionService.save(mission)>0){
			/*根据逗号分解userid*/
			if(mission.getUser() != null && mission.getUser() != ""){
				String [] userids = mission.getUser().split(",");
				for (String userid:userids) {
					/*添加中间表*/
					UserMissionDO userMissionDO = new UserMissionDO();
					userMissionDO.setUserid(Long.valueOf(userid));
					userMissionDO.setMissionid(mission.getId());
					userMissionDO.setStatus(1l);
					userMissionService.save(userMissionDO);
				}
			}

			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("contract:mission:edit")
	public R update( MissionDO mission){
		missionService.update(mission);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("contract:mission:remove")
	public R remove( Long id){
		if(missionService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("contract:mission:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		missionService.batchRemove(ids);
		return R.ok();
	}
	
}
